using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using MULENetworking;


namespace MULEServer
{
    public class RandomEvent
    {
        private Random rand;
        private Boolean[] usedList; // list of which events have been used;

        public RandomEvent()
        {
            rand = new Random();
            usedList = new Boolean[22];

            for (int x = 0; x < 22; x++) usedList[x] = false;
        }

        // will return "" if no event is generated.
        public String generatePlayerEvent(IPlayer player, int turn)
        {
            int selectEvent;
            int modifier = (25 * (turn/4) + 1);
            int doEvent = rand.Next(4);
            if (doEvent != 0) return "";
            else
            {
                selectEvent = rand.Next(22);
                if (usedList[selectEvent] == true) return "";

                switch (selectEvent)
                {
                    case 0: // 0 - 12 are good
                        if (player.place > 1)
                        {
                            player.food += 3;
                            player.energy += 2;
                            usedList[0] = true;
                            return "You just got a package from the Tech Alumni Club containing 3 food and 2 energy.";
                        }
                        else return "";
                    case 1:
                        if (player.place > 1)
                        {
                            player.smithore += 2;
                            usedList[1] = true;
                            return "George P. Burdell wandered by and repaid your hospitality by leaving you 2 units of smithore.";
                        }
                        else return "";
                    case 2:
                        if (player.place > 1)
                        {
                            if (player.hasMule == true)
                            {
                                player.money += (2 * modifier);
                                usedList[2] = true;
                                return ("Your mule was judged best built by the Mechanical Engineering department.  You won " + (2 * modifier) + " credits!");
                            }
                            else return "";
                        }
                        else return "";
                    case 3:
                        if (player.place > 1)
                        {
                            if (player.hasMule == true)
                            {
                                player.money += (4 * modifier);
                                usedList[3] = true;
                                return ("Your mule won the colony robo-soccer tournament. You won " + (4 * modifier) + " credits.");
                            }
                            else return "";
                        }
                        else return "";
                    case 4:
                        if (player.place > 1)
                        {
                            int numberOfFoodPlots = 0;
                            for (int x = 0; x < player.plotsOwned.Count; x++)
                            {
                                IMapSquare tempSquare = (IMapSquare)player.plotsOwned[x];
                                if (tempSquare.Mule == "food")
                                {
                                    numberOfFoodPlots++;
                                }
                            }
                            if (numberOfFoodPlots < 0)
                            {
                                player.money += (2 * modifier * numberOfFoodPlots);
                                usedList[4] = true;
                                return ("The colony council for agriculture awarded you " + (2 * modifier * numberOfFoodPlots) + " credits for your food production research.");
                            }
                            else return "";
                        }
                        else return "";
                    case 5:
                        if (player.place > 1)
                        {
                            player.money += 4 * modifier;
                            return ("The colony awards you " + (4 * modifier) + " credits stopping the wart worm infestation.");
                        }
                        else return "";
                    case 6:
                        if (player.place > 1)
                        {
                            player.money += 8 * modifier;
                            return ("The museum of lost PC's awards you " + (8 * modifier) + " credits for your old atari computer.");
                        }
                        else return "";
                    case 7:
                        if (player.place > 1)
                        {
                            player.money += 2 * modifier;
                            return ("You won the 5 day old pizza eating contest and collected " + (2 * modifier) + " credits.  YUCK!!!");
                        }
                        else return "";
                    case 8:
                        if (player.place > 1)
                        {
                            player.money += 3 * modifier;
                            return ("Your homeworld held a charity concert for you and sent " + (3 * modifier) + " credits.");
                        }
                        else return "";
                    case 9:
                        if (player.place > 1)
                        {
                            player.money += 6 * modifier;
                            return ("Your off-world investments in artifical dumbness paid " + (6 * modifier) + " credits in dividends.");
                        }
                        else return "";
                    case 10:
                        if (player.place > 1)
                        {
                            player.money += 4 * modifier;
                            return ("A distant relative died and left you a vast fortune.  Unfortunately, after the democrats \"soaked the rich\", you were left with only " + (4 * modifier) + " credits.");
                        }
                        else return "";
                    case 11:
                        if (player.place > 1)
                        {
                            player.money += 2 * modifier;
                            return ("You found UGA the dog and returned him for a " + (2 * modifier) + " credit reward.");
                        }
                        else return "";
                    case 12:
                        if (player.place > 1)
                        {
                            // NYI: getting an extra plot of land.
                            return ("You received an extra plot of land to encourage colony development.");
                        }
                        else return "";
                    case 13:
                        if (player.place < 4)
                        {
                            player.food = (player.food / 2);
                            return ("UGA frat boys had a party and stole half your food.");
                        }
                        else return "";
                    case 14:
                        if (player.place < 4)
                        {
                            player.money -= (3 * modifier);
                            return ("One of your MULE's lost a screw.  Repairs cost you " + (3 * modifier) + " credits.");
                        }
                        else return "";
                    case 15:
                        if (player.place < 4)
                        {
                            int numberOfMineMules = 0;
                            for (int x = 0; x < player.plotsOwned.Count; x++)
                            {
                                IMapSquare tempSquare = (IMapSquare)player.plotsOwned[x];
                                if (tempSquare.Mule == "smithore")
                                {
                                    numberOfMineMules++;
                                }
                            }
                            if (numberOfMineMules > 0)
                            {
                                player.money -= (2 * modifier * numberOfMineMules);
                                return ("Your mining MULE's deteriorate from heavy use.  Repairs cost you " + (2 * modifier * numberOfMineMules) + " credits to repair.");
                            }
                            else return "";
                        }
                        else return "";
                    case 16:
                        if (player.place < 4)
                        {
                            int numberOfEnergyMules = 0;
                            for (int x = 0; x < player.plotsOwned.Count; x++)
                            {
                                IMapSquare tempSquare = (IMapSquare)player.plotsOwned[x];
                                if (tempSquare.Mule == "smithore")
                                {
                                    numberOfEnergyMules++;
                                }
                            }
                            if (numberOfEnergyMules > 0)
                            {
                                player.money -= (modifier * numberOfEnergyMules);
                                return ("Your energy MULE's are dirty.  Cleaning costs " + (modifier * numberOfEnergyMules) + " credits.");
                            }
                            else return "";
                        }
                        else return "";
                    case 17:
                        if (player.place < 4)
                        {
                            player.money -= 6 * modifier;
                            return ("Your UGA grad brother in law made a mess of town.  It costs " + (6 * modifier) + " to clean it up.");
                        }
                        else return "";
                    case 18:
                        if (player.place < 4)
                        {
                            player.money -= 4 * modifier;
                            return ("Buzz went beserk and ate the roof off your house.  Repairs cost " + (4 * modifier) + " credits.");
                        }
                        else return "";
                    case 19:
                        if (player.place < 4)
                        {
                            player.money -= 4 * modifier;
                            return ("You lost " + (4 * modifier) + " credits betting that P==NP.");
                        }
                        else return "";
                    case 20:
                        if (player.place < 4)
                        {
                            player.money -= 4 * modifier;
                            return ("Your child was bitten by UGA the dog.  The hospital takes " + (4 * modifier) + " to patch them up.");
                        }
                        else return "";
                    case 21:
                        if (player.place < 4)
                        {
                            if (player.plotsOwned.Count >= 1)
                            {
                                int plotToLose = rand.Next(player.plotsOwned.Count);
                                //remove the ownership of the square on the map NYI
                                IMapSquare removedPlot = (IMapSquare)player.plotsOwned[plotToLose];
                                player.plotsOwned.RemoveAt(plotToLose);
                                return ("You lost a plot of land because the claim was not recorded properly.");
                            }
                            else return "";
                        }
                        else return "";
                    default:
                        return "";
                }

            }
        }
    }
}
